package com.ruben.simplescaffold.pojo.dto;

import cn.hutool.core.lang.Opt;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruben.simplescaffold.constant.PageParamConst;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 公共接参
 *
 * @author <achao1441470436@gmail.com>
 * @since 2021/6/25 10:03
 */
@Data
@AllArgsConstructor
public class CommonDTO implements Serializable {
    private static final long serialVersionUID = 4319499896342099805L;
    /**
     * 当前页
     */
    private Long current;
    /**
     * 每页显示条数，默认 10
     */
    private Long size;
    /**
     * 关键字(无搜索条件时有时作为用户id)
     */
    private String keywords;
    /**
     * 类型
     */
    private String searchType;

    /**
     * 起始时间
     */
    private LocalDateTime start;

    /**
     * 结束时间
     */
    private LocalDateTime end;

    public CommonDTO() {
        this.current = PageParamConst.DEFAULT_CURRENT;
        this.size = PageParamConst.DEFAULT_SIZE;
    }


    public CommonDTO(Long size) {
        this.current = PageParamConst.DEFAULT_CURRENT;
        this.size = size;
    }

    public CommonDTO(Long current, Long size) {
        this.current = current;
        this.size = size;
    }

    /**
     * 转换成mp查询page对象
     *
     * @return com.baomidou.mybatisplus.extension.plugins.pagination.Page<T>
     * @author <achao1441470436@gmail.com>
     * @date 2021/5/5 0005 15:38
     */
    public <T> Page<T> toPage() {
        return new Page<>(Opt.ofNullable(current).orElse(PageParamConst.DEFAULT_CURRENT),
                Opt.ofNullable(size).orElse(PageParamConst.DEFAULT_SIZE));
    }

}
